﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SmlEngine.Graphics
{
    public class BitmapFontHandler
    {
        private Texture2D fontTexture;
        private Dictionary<char, Texture2D> characters;

        public BitmapFontHandler(ContentManager Content, string bitmapFontSource, Dictionary<char, Rectangle> charMappings)
        {
            characters = new Dictionary<char, Texture2D>();
            fontTexture = Content.Load<Texture2D>(bitmapFontSource);
            foreach (var value in charMappings)
            {
                characters.Add(value.Key, GraphicsHelper.Crop(fontTexture, value.Value));
            }
        }

        public void Draw(SpriteBatch spriteBatch, string drawString, Vector2 drawPosition)
        {
            if (string.IsNullOrEmpty(drawString))
            {
                return;
            }

            Vector2 charSize = new Vector2(characters['a'].Width, characters['a'].Height);
            Vector2 currentPosition = drawPosition;
            for (int i = 0; i < drawString.Length; i++)
            {
                if (characters.ContainsKey(drawString[i]))
                {
                    spriteBatch.Draw(characters[drawString[i]], currentPosition, Color.Black);
                }
                currentPosition.X += charSize.X;
            }

        }
    }
}
